<template>
	<view>
		<view class="list">
			<view class="list-one l" v-for="(item, list) in consultlist" :key="list" @click="open_details(item)">
				<view class="listimg">
					<u--image width="310rpx" height="240rpx" :src="item.pic" radius="4">
						<template v-slot:loading>
							<u-loading-icon color="#39488a"></u-loading-icon>
						</template>
					</u--image>
				</view>
				<view class="name one">{{ item.njType }}</view>
				<view class="mony">
					租金：
					<u--text mode="price" :text="item.price" block color="red" style="width: auto; flex: inherit; margin-right: 8rpx" bold></u--text>
					元/天
				</view>
				<view class="time">可租赁时间：</view>
				<view class="time" style="font-weight: bold">{{ item.startTime.substring(0, 10) }}-{{ item.endTime.substring(0, 10) }}</view>
			</view>
			<view class="clear"></view>
		</view>
		<u-empty :show="consultlist.length == 0 && loading ? true : false" marginTop="100"></u-empty>
		<u-loadmore :status="loadstatus" line v-if="consultlist.length > 0" />
		<view style="height: 140rpx"></view>
		<view class="btn" @click="open_popup">
			<view class="topval">发布</view>
			<view class="bottmval">农具租赁</view>
		</view>
		<u-popup :show="show" @close="close" :round="10" closeable>
			<view class="popup-tit">发布农具租赁</view>
			<view class="popup-cont">
				<view class="tit">农具名称</view>
				<view class="text">
					<u--input v-model="formdata.njType" placeholder="请输入农具名称"></u--input>
				</view>
				<view class="tit">价格</view>
				<view class="text">
					<u--input v-model="formdata.price" placeholder="请输入价格"></u--input>
				</view>
				<view class="tit">数量</view>
				<view class="text">
					<u--input v-model="formdata.num" placeholder="请输入数量"></u--input>
				</view>
				<view class="tit">租赁时段</view>
				<view class="text" @click="timeShow = true">
					<u--input placeholder="请选择租赁时段" :value="formdata.startTime + '-' + formdata.endTime"></u--input>
				</view>

				<view class="tit">联系人</view>
				<view class="text">
					<u--input v-model="formdata.contactNickname" placeholder="请输入联系人"></u--input>
				</view>
				<view class="tit">联系电话</view>
				<view class="text">
					<u--input v-model="formdata.contactMobile" placeholder="请输入联系电话"></u--input>
				</view>
				<view class="tit">备注</view>
				<view class="text">
					<u--textarea v-model="formdata.remark" placeholder="请输入备注" count></u--textarea>
				</view>
				<view class="tit">照片</view>
				<u-upload
					:fileList="fileList2"
					@afterRead="afterRead"
					@delete="deletePic"
					:previewFullImage="false"
					name="2"
					:maxCount="1"
					accept="image"
					uploadText="图片"
					:sizeType="['compressed']"
				></u-upload>
				<view style="color: red; font-size: 24rpx" v-if="errtext">{{ errtext }}</view>
				<view class="btns" @click="click_idea">发布</view>
			</view>
		</u-popup>
		<u-calendar
			:show="timeShow"
			mode="range"
			@confirm="confirmTime"
			startText="开始"
			endText="结束"
			confirmDisabledText="请选择租赁时段"
			@close="timeShow = false"
		></u-calendar>
	</view>
</template>

<script>
import { njzpfbpage, njzpfbcreate } from '@/api/index.js';
import { commonUpload } from '@/api/mine.js';
export default {
	data() {
		return {
			consultlist: [],
			queryParams: {
				pageNo: 1,
				pageSize: 10
			},
			show: false,
			fileList2: [],
			loading: false,
			loadstatus: 'loading',
			formdata: {
				njType: '锄头',
				price: 9.8,
				num: 10,
				startTime: '',
				endTime: '',
				contactNickname: '王五',
				contactMobile: '13000000000',
				remark: '你说的对',
				pic: ''
			},
			timestartend: [],
			timeShow: false,
			errtext: ''
		};
	},
	onLoad() {
		this.get_zjzx();
	},
	methods: {
		open_popup() {
			this.show = true;
			this.formdata = {
				njType: '',
				price: '',
				num: '',
				startTime: '',
				endTime: '',
				contactNickname: '',
				contactMobile: '',
				remark: '',
				pic: ''
			};
		},
		confirmTime(e) {
			this.timeShow = false;
			this.formdata.startTime = e[0];
			this.formdata.endTime = e[e.length - 1];
		},
		click_idea() {
			if (
				!this.formdata.njType ||
				!this.formdata.price ||
				!this.formdata.num ||
				!this.formdata.startTime ||
				!this.formdata.endTime ||
				!this.formdata.contactNickname ||
				!this.formdata.contactMobile ||
				!this.formdata.remark
			) {
				uni.$u.toast('请填写内容');
				// this.errtext = '请填写内容';
				return;
			}
			if (this.fileList2.length == 0) {
				this.errtext = '请选择图片';
				return;
			}
			this.formdata.pic = this.fileList2[0].url;
			njzpfbcreate(this.formdata).then((res) => {
				if (res.code == '0') {
					uni.$u.toast('发布成功');
					this.queryParams.pageNo = 1;
					this.consultlist = [];
					this.get_zjzx();
					this.close();
				} else {
					uni.$u.toast(res.msg);
					// this.errtext = res.msg
				}
			});
		},
		close() {
			this.show = false;
			this.fileList2 = [];
			this.errtext = '';
		},
		get_zjzx() {
			njzpfbpage(this.queryParams).then((res) => {
				this.loading = true;
				if (res.data.list.length == 0 && this.queryParams.pageNo > 1) {
					this.loadstatus = 'nomore';
					return;
				}
				if (this.queryParams.pageNo == 1 && res.data.list.length < 10) {
					this.loadstatus = 'nomore';
				}
				this.consultlist = this.consultlist.concat(res.data.list);
				this.queryParams.pageNo = res.data.list.length == 0 ? this.queryParams.pageNo : (this.queryParams.pageNo += 1);
			});
		},
		open_details(list) {
			uni.navigateTo({
				url: '/pages/index/tool/details/details?id=' + list.id
			});
		},
		// 删除图片
		deletePic(event) {
			this[`fileList${event.name}`].splice(event.index, 1);
		},
		// 新增图片
		async afterRead(event) {
			// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
			let lists = [].concat(event.file);
			let fileListLen = this[`fileList${event.name}`].length;
			lists.map((item) => {
				this[`fileList${event.name}`].push({
					...item,
					status: 'uploading',
					message: '上传中'
				});
			});
			for (let i = 0; i < lists.length; i++) {
				const result = await this.uploadFilePromise(lists[i].url);
				let item = this[`fileList${event.name}`][fileListLen];
				this[`fileList${event.name}`].splice(
					fileListLen,
					1,
					Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					})
				);
				fileListLen++;
			}
		},
		uploadFilePromise(url) {
			return new Promise((resolve, reject) => {
				let a = commonUpload({
					url: url
				}).then((res) => {
					if (res.code == '0') {
						setTimeout(() => {
							resolve(res.data);
						}, 50);
					} else {
						uni.$u.toast('上传失败');
					}
				});
			});
		}
	},
	onPullDownRefresh() {
		this.queryParams.pageNo = 1;
		this.consultlist = [];
		this.get_zjzx();
	},
	onReachBottom() {
		this.get_zjzx();
		this.loadstatus = 'loading';
	}
};
</script>

<style lang="scss" scoped>
.list {
	padding: 24rpx;

	.list-one {
		padding-bottom: 20rpx;
		width: 340rpx;
		border-radius: 20rpx;
		background-color: #ffffff;
		overflow: hidden;
		margin-bottom: 20rpx;

		.listimg {
			padding: 12rpx;
		}

		.name {
			padding: 0 12rpx;
			line-height: 40rpx;
			font-size: 28rpx;
			color: #333333;
			font-weight: bold;
		}

		.mony {
			line-height: 44rpx;
			color: #666666;
			font-size: 26rpx;
			display: flex;
			padding: 0 12rpx;
		}

		.time {
			margin-top: 4rpx;
			font-size: 24rpx;
			padding: 0 12rpx;
		}
	}

	.list-one:nth-child(even) {
		margin-left: 20rpx;
	}
}

.btn {
	position: fixed;
	top: 50%;
	z-index: 9;
	right: 12rpx;
	width: 182rpx;
	height: 206rpx;
	text-align: center;
	background: url('../../../static/index/btn.png') no-repeat;
	background-size: 100% 100%;
	color: #3c6d09;
	font-size: 24rpx;

	.topval {
		text-indent: 1em;
		margin-top: 120rpx;
		line-height: 36rpx;
	}

	.bottmval {
		text-indent: 1em;
		line-height: 36rpx;
	}
}

.popup-tit {
	line-height: 44rpx;
	padding-top: 48rpx;
	text-align: center;
}

.popup-cont {
	padding: 24rpx 48rpx 48rpx;
	max-height: 800rpx;
	overflow: auto;

	.tit {
		text-align: left;
		font-size: 26rpx;
		line-height: 44rpx;
		font-weight: bold;
		margin-top: 12rpx;
		margin-bottom: 12rpx;
	}

	.text {
		margin: 0;
		border: 2rpx solid #d8d8d8;
		border-radius: 12rpx;
	}

	.btns {
		margin: 64rpx;
		height: 88rpx;
		background: #8bb950;
		border-radius: 20rpx;
		color: #fff;
		text-align: center;
		line-height: 88rpx;
	}
}
</style>
